import 'package:flutter/material.dart';
import 'package:flutter_app/bean/problem_report_type_entity.dart';
import 'package:flutter_app/r.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../query_list_logic.dart';

/**
 * @Description: 填报问题 清单列表 listItem
 * @Author: 下雨了
 * @CreateDate: 2022-7-15 11:11:49
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-7-15 11:11:49
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
class QueryListItemPage extends StatefulWidget {
  final int index;
  final ProblemTypeList? list;

  const QueryListItemPage({Key? key, required this.index, this.list}) : super(key: key);

  @override
  _QueryListItemPageState createState() => _QueryListItemPageState();
}

class _QueryListItemPageState extends State<QueryListItemPage> {
  final queryLogic = Get.find<QueryListLogic>();

  late int isChoose = 0; // 1 存在 0不存在

  @override
  void initState() {
    super.initState();
    if (int.parse(widget.list?.problemCount ?? "0") > 0) {
      isChoose = 1;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 16.r, right: 16.r),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(4.r),
        boxShadow: [
          BoxShadow(color: Color(0x1A5F5F5F),),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(left: 18.r, right: 18.r, top: 18.r, bottom: 18.r),
            child: Text(
              "${(widget.index + 1)}.${widget.list?.name}",
              maxLines: 2,
              style: TextStyle(color: MyColors.FF333333, fontSize: 16.sp, fontWeight: FontWeight.w500,),
            ),
          ),
          Container(
            margin: EdgeInsets.only(left: 24.r, right: 24.r, bottom: 20.r),
            child: IgnorePointer(
              ignoring: queryLogic.memberStatus != "0",
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ExistItem(
                    isChoose: isChoose == 1,
                    title: '存在',
                    onTap: () {
                      setState(() {
                        isChoose = 1;
                      });
                    },
                  ),
                  ExistItem(
                    isChoose: isChoose == 0,
                    title: '不存在',
                    onTap: () {
                      setState(() {
                        isChoose = 0;
                      });
                    },
                  ),
                ],
              ),
            ),
          ),
          Visibility(
            visible: isChoose == 1,
            child: Container(
              color: Colors.white,
              margin: EdgeInsets.only(bottom: 18.r),
              child: ListView.separated(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                padding: EdgeInsets.only(top: 0),
                itemCount: widget.list?.childTypeList?.length ?? 0,
                itemBuilder: (BuildContext context, int index) {
                  return ListItem(widget.list?.childTypeList![index]);
                },
                separatorBuilder: (BuildContext context, int index) {
                  return Divider(color: Colors.transparent, height: 5.r,);
                },
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 展开后的item
  Widget ListItem(ListChildTypeList? item) {
    return Container(
      margin: EdgeInsets.only(right: 18.r, left: 18.r),
      padding: EdgeInsets.only(left: 18.r, right: 18.r),
      height: 55.r,
      decoration: BoxDecoration(
        color: MyColors.FFF7F7F7,
        borderRadius: BorderRadius.circular(4.r)
      ),
      child: InkWell(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Flexible(
              child: Text(item?.name ?? "",
                maxLines: 2,
                style: TextStyle(color: MyColors.FF333333, fontSize: 14.sp),)
            ),
            Row(
              children: [
                Text("${(int.parse(item?.problemCount ?? "0"))}项", style: TextStyle(color: MyColors.FF666666, fontSize: 12.sp),),
                SizedBox(width: 11.r,),
                Image.asset(R.assetsImgIcon48, width: 10.r,),
              ],
            ),
          ],
        ),
        onTap: () {
          queryLogic.goToQueryDetailPage(item?.name, item?.code);
        },
      ),
    );
  }

  // 左右切换按钮
  Widget ExistItem({
    required String title,
    required bool isChoose,
    Color? chooseColor = Colors.red,
    VoidCallback? onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: 140.r,
        height: 32.r,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          border: isChoose ? Border.all(color: MyColors.FF2763B9) : Border.all(color: MyColors.FF999999),
          borderRadius: BorderRadius.circular(16.r),
          color: isChoose ? MyColors.FFDFE6F8 : Colors.white,
        ),
        child: Text(
          title,
          style: TextStyle(color: isChoose ? MyColors.FF2763B9 : MyColors.FF666666, fontSize: 14.sp),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
